<?php

class Customer extends BaseController {

    public $customer;

    function __construct() {
        parent::__construct();
        $this->customer = new MCustomer();
    }

    //添加或修改客户
    function inUp() {
        $data = Input::postString('data');
        $data = json_decode($data,true);
        foreach ($data as $key => $value) {
            if(empty($value)){
                unset($data[$key]);
            }
        }
        $add_result = $this->customer->inUp($data);
        if ($add_result > 0) {
            $message = isset($data['id'])?"修改成功":'添加成功';
            $this->jsonMsg(200, $message, $add_result);
        }
    }
    
    //添加列表
    function index() {
        $search = Input::get('search');
        $page = Input::getInt('page',1);
        $page = $page<1?1:$page;
        $page_size = 15;
        
        $customer_count = $this->customer->selectCounts($search,$page);
        $customer = $this->customer->selectLists($search,$page,$page_size);
        $this->assign['search'] = $search;
        $this->assign['page_count'] = ceil($customer_count['ct']/$page_size);
        $this->assign['customer_lists'] = isset($customer)?$customer:'';
        $this->assign['include_page'] = __TPL_PATH__ . 'Customer/index.html';
        $template_path = __TPL_PATH__ . 'index.html';
        $this->_view->display($template_path, $this->assign);
    }

    //添加客户
    function add() {
        $title = "新增";
        $this->assign['title'] = $title;
        $this->assign['customer'] = '';
        $this->assign['include_page'] = __TPL_PATH__ . 'Customer/add.html';
        $template_path = __TPL_PATH__ . 'index.html';
        $this->_view->display($template_path, $this->assign);
    }
    
    //修改客户
    function update() {
        $id = Input::getInt('id');
        $title = "修改";
        $customer = $this->customer->selectInfo($id);
        $this->assign['title'] = $title;
        $this->assign['id'] = $id;
        $this->assign['customer'] = $customer;
        $this->assign['include_page'] = __TPL_PATH__ . 'Customer/add.html';
        $template_path = __TPL_PATH__ . 'index.html';
        $this->_view->display($template_path, $this->assign);
    }

    //客户搜索
    function search() {
        $search = Input::get('search');
        $page = Input::getInt('page', 1);
        $page_size = 5;

        $retun = array();
        $total_count = $this->customer->search($search, "count(*) as ct", 1);
        $retun['total_count'] = $total_count['ct'];

        $customers = $this->customer->search($search, '*', 0, $page, $page_size);
        foreach ($customers as $key => $c) {
            $retun['items'][] = array(
                'id' => $c['id'],
                'text' => $c['name']
            );
        }
        $this->jsonMsg(200, '', $retun);
    }
    
    //获取客户详情
    function getInfoById() {
        $customer_id = Input::getInt('id');
        $add_result = $this->customer->selectInfo($customer_id);
        $this->jsonMsg(200, '请求成功', $add_result);
    }
    
    //删除客户
    public function delCus(){
        $id = Input::postInt('id');
        //删除客户
        $result = $this->customer->del($id);
        if($result){
            $this->jsonMsg(200, "删除客户成功",$result);
        }  else {
            $this->jsonMsg(100, "删除客户失败");
        }
    }

}
